131B - Opposites Attract - CodeForces Solution


implementation math *1200

Please click on ads to support us..

Python Code:

n = input()
pari = {}
paruDZ = 0
saraksts = list(map(int,input().split()))
for i in saraksts:
  if i in pari:
    pari[i]+=1
  else:
    pari[i] = 1

for k in pari:
  if k == 0 :
    paruDZ +=  pari[k] * (pari[k] - 1)
  elif k * -1 in pari:
    
    paruDZ += pari[k] * pari[k * -1]

print(paruDZ //2)

C++ Code:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string.h>
#include<bitset>
#include<cmath>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<string>
#include<cstdlib> 
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define test_case int t;cin>>t;while(t--)
typedef long long int ll;
typedef long double ld;
using namespace std;
void Abdo_Saad() { std::ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL); }
ll gcd(ll a, ll b) { return(b == 0 ? a : gcd(b, a % b)); }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
void set_bit(int& n, int idx) { n |= 1 << idx; } 
void reset_bit(int& n, int idx) { n &= ~(1 << idx); }
void flib_bit(int& n, int idx) { n ^= 1 << idx; }
void solve()
{
	int n;
	cin >> n;
	vector<int>v(n),freq(11);
	for (int i = 0;i < n;i++)
	{
		cin >> v[i];
		if(v[i]>=0)freq[v[i]]++;
	}
	ll cnt = 0;
	for (int i = 0;i < n;i++)
	{
		if (v[i] < 0)cnt += freq[v[i] * -1];
	}
	cout << cnt + ll(freq[0]) * (freq[0] - 1) / 2;
	
}
int main()
{
	Abdo_Saad();
	//freopen("strings.in", "r", stdin);
	//freopen(".out","w",stdout);
	//test_case
	solve();
}


Comments

Submit
0 Comments
More Questions

1395A - Boboniu Likes to Color Balls
1637C - Andrew and Stones
1334B - Middle Class
260C - Balls and Boxes
1554A - Cherry
11B - Jumping Jack
716A - Crazy Computer
644A - Parliament of Berland
1657C - Bracket Sequence Deletion
1657B - XY Sequence
1009A - Game Shopping
1657A - Integer Moves
230B - T-primes
630A - Again Twenty Five
1234D - Distinct Characters Queries
1183A - Nearest Interesting Number
1009E - Intercity Travelling
1637B - MEX and Array
224A - Parallelepiped
964A - Splits
1615A - Closing The Gap
4C - Registration System
1321A - Contest for Robots
1451A - Subtract or Divide
1B - Spreadsheet
1177A - Digits Sequence (Easy Edition)
1579A - Casimir's String Solitaire
287B - Pipeline
510A - Fox And Snake
1520B - Ordinary Numbers